package cn.edu.cug.cs.gtl.renders.openinventor.callbacks;

import cn.edu.cug.cs.gtl.apis.render.Selector;
import cn.edu.cug.cs.gtl.apis.render.nodes.Node;
import cn.edu.cug.cs.gtl.renders.openinventor.nodes.SeparatorImpl;
import com.openinventor.inventor.SbColor;
import com.openinventor.inventor.SoPath;
import com.openinventor.inventor.fields.SoMFColor;
import com.openinventor.inventor.misc.callbacks.SoSelectionPathCB;
import com.openinventor.inventor.nodes.*;



public class MySelectionPathCallback extends SoSelectionPathCB {
    Selector selector =null;

    public MySelectionPathCallback(Selector c) {
        this.selector=c;
    }
    @Override
    public void invoke(SoPath p ) {

        SoNode t = p.regular.getTail();

        if (t instanceof SoIndexedFaceSet
                || t instanceof SoIndexedLineSet
                || t instanceof SoIndexedPointSet) {

            int l = p.regular.getLength();
            if(l>=2) {
                SoNode n = p.regular.getNodeFromTail(1);
                if (n instanceof SoSeparator) {
                    //添加到选择器中
                    for(int i=0;i<((SoSeparator) n).getNumChildren();++i){
                        SoNode n1 = ((SoSeparator) n).getChild(i);
                        if (n1 instanceof SoMaterial) {
                            Node  selectedNode= new SeparatorImpl((SoSeparator) n);
                            SoMFColor oldColor = ((SoMaterial) n1).diffuseColor;
                            SbColor [] cs = oldColor.getValues(0);
                            selectedNode.setUserData(cs);
                            selector.add(selectedNode);
                            ((SoMaterial) n1).diffuseColor.setValue((SbColor) selector.getSelectedColor().getNativeObject());
                            break;
                        }
                    }
                }
            }
        }
    }


}